package com.aiju.widgit.menu;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.common.PixValue;
import com.aiju.widgit.layout.BorderLinearLayout;

/**
 * @author: 佐为
 * @date: 2016-11-08 18:43
 * @version: V1.0
 * @Description: 菜单分组
 */
public class ListMenuView extends LinearLayout {

    private Context mContext;
    private TextView mTextView; //分组标题
    private BorderLinearLayout mBorderLinearLayout;
    private LayoutInflater mLayoutInflater;

    /**
     * 点击回调
     */
    private OnListMenuClickListener listener;

    public interface OnListMenuClickListener {
        void onListMenuClick(int position);
    }

    public ListMenuView(Context context, ListMenuGroup takeGroup, OnListMenuClickListener onListMenuClickListener) {
        super(context);
        this.listener = onListMenuClickListener;
        init(context);
        showListMenuView(takeGroup);
    }

    private void init(Context context) {
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(Color.WHITE);
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);

        mTextView = new TextView(context);
        mTextView.setBackgroundColor(getResources().getColor(R.color.app_background_color));
        mTextView.setTextColor(getResources().getColor(R.color.app_text_color));
        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, PixValue.dp.valueOf(45)));

        mBorderLinearLayout = new BorderLinearLayout(context);
        mBorderLinearLayout.setOrientation(LinearLayout.VERTICAL);
        mBorderLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mBorderLinearLayout.setBorderColor(getResources().getColor(R.color.app_divider_color));
        mBorderLinearLayout.setBorderSize(0, PixValue.dp.valueOf(0.5f), 0, PixValue.dp.valueOf(0.5f));
        mBorderLinearLayout.setShowDividers(SHOW_DIVIDER_MIDDLE);
        mBorderLinearLayout.setDividerDrawable(getResources().getDrawable(R.drawable.shape_list_divider_15));
    }

    /**
     * 设置菜单数据
     *
     * @param group
     */
    public void showListMenuView(final ListMenuGroup group) {
        if (group == null || group.getItems() == null) {
            return;
        }
        if (TextUtils.isEmpty(group.getTitle())) {
            ViewGroup.LayoutParams layoutParams = mTextView.getLayoutParams();
            layoutParams.height = PixValue.dp.valueOf(20);
        }
        mTextView.setText(group.getTitle());
        addView(mTextView);

        for (final ListMenuItem item : group.getItems()) {
            View itemView = mLayoutInflater.inflate(R.layout.item_list_menu, mBorderLinearLayout, false);

            ImageView head = (ImageView) itemView.findViewById(R.id.iv_banner_item_head);
            ImageView end = (ImageView) itemView.findViewById(R.id.iv_banner_item_end);
            TextView title = (TextView) itemView.findViewById(R.id.tv_banner_item_title);

            head.setImageResource(item.getLeftImage());
            end.setImageResource(item.getRightImage());
            title.setText(item.getItemTitle());

            if (!TextUtils.isEmpty(item.getItemDetail())) {
                TextView detail = (TextView) itemView.findViewById(R.id.tv_banner_item_detail);
                detail.setVisibility(View.VISIBLE);
                detail.setText(item.getItemDetail());
            }

            itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.onListMenuClick(group.getItems().indexOf(item));
                    }
                }
            });

            mBorderLinearLayout.addView(itemView);
        }
        addView(mBorderLinearLayout);
    }

    /**
     * 设置点击事件
     *
     * @param listener
     */
    public void setOnListMenuClickListener(OnListMenuClickListener listener) {
        this.listener = listener;
    }
}
